package com.bjsxt.controller;


import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjsxt.pojo.HostAddResult;
import com.bjsxt.pojo.MarriedPerson;
import com.bjsxt.pojo.PageResult;
import com.bjsxt.service.IMarriedPersonService;
import com.bjsxt.util.AliPhoneUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;
import java.sql.Date;
import java.time.LocalDate;
import java.time.LocalDateTime;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author ${author}
 * @since 2021-10-14
 */
@Controller
@RequestMapping("/marriedPerson")
public class MarriedPersonController {
    //声明业务层
    @Autowired
    private IMarriedPersonService marriedPersonService;

    @ResponseBody
    @RequestMapping("marriedPersonInfo")
    public PageResult<MarriedPerson> marriedPersonInfo(Integer page,Integer rows,String pname,String pphone){
        //处理请求
        IPage<MarriedPerson> p = new Page<>(page,rows);
        PageResult<MarriedPerson> pageResult = marriedPersonService.selMarriedPersonInfo(page, rows, pname, pphone);
        return pageResult;
    }

    @ResponseBody
    @RequestMapping("reg")
    public HostAddResult reg(MarriedPerson marriedPerson){
        //处理请求
        marriedPerson.setMarrydate(Date.valueOf(LocalDate.now()));
        marriedPerson.setRegdate(LocalDateTime.now());
        marriedPerson.setStatus("1");
        boolean insert = marriedPerson.insert();
        //响应结果
        return new HostAddResult(insert,"");
    }

    @ResponseBody
    @RequestMapping("login")
    public MarriedPerson login(MarriedPerson marriedPerson, HttpSession session){
        QueryWrapper<MarriedPerson> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("pphone",marriedPerson.getPphone()).eq("ppwd",marriedPerson.getPpwd());
        //处理请求
        MarriedPerson marriedPerson1 = marriedPerson.selectOne(queryWrapper);
        session.setAttribute("marriedPerson",marriedPerson1);
        //响应结果
        return marriedPerson1;
    }
    private Integer newCode;
    //手机验证码
    @ResponseBody
    @RequestMapping("personCode")
    public String personCode(String phonenumber,HttpSession session) throws ClientException {
        //得到验证码
        AliPhoneUtil.setNewCode();
        newCode = AliPhoneUtil.getNewCode();
        SendSmsResponse sendSmsResponse = AliPhoneUtil.sendSms(phonenumber,newCode);
        System.out.println(sendSmsResponse);
        System.out.println(newCode);
        session.setAttribute("newCode",newCode);
        return sendSmsResponse.getCode();
    }
}

